<template>
	<view class="box">
		<view class="logs_img">
			<image :src="logo" mode="widthFix"></image>
		</view>
		<!-- <button class="power_button"  @click="WeChatUserInfo">一键登录</button> -->
		<!-- 	<button class="power_button"  open-type="chooseAvatar" >快捷登录</button> -->

		<view class="power_button" v-if="!check" @click="setCheck()">手机号快捷登录</view>

		<button v-else class="power_button" @click="setShow">手机号快捷登录</button>

		<!--  -->

		<view class="agree ag_box">
			<view class="iconfont fs-18" :class="check?'icondanxuanfuxuan':'iconico2'" @click="check=!check"></view>
			<view class="text_box">
				<view>我已阅读并同意</view>
				<view v-for="(item,index) in xyList" :key="index" @click="pageUrl(item)" class="protocol">{{item.title}}
				</view>
			</view>
		</view>

		<u-popup v-model="show" :mask-close-able="true" z-index="50" :mask="true" mode="bottom" border-radius="14"
			:closeable="true" close-icon-pos="top-right">
			<view class="msgs_box">
				<view class="msg_head">
					<image class="logo_img" :src="logo"></image>
					<view>叮咚当家</view>
					<view class="ml">申请</view>
				</view>
				<view class="msg_text">获取你的昵称、头像和权限</view>


				<button class="tx_box" open-type="chooseAvatar" @chooseavatar="setImg">
					<view>头像</view>
					<image v-if="!img" src="@/static/img/avatar.png" class="msg_img"></image>
					<image v-if="img" :src="img" class="msg_img"></image>
					<u-icon class="msg_right" name="arrow-right" color="#363637" size="28"></u-icon>

				</button>

				<view class="tx_box">
					<view>昵称</view>
					<input v-model="form.nickname" class="ml" type="nickname" placeholder="请填写昵称" />
				</view>

				<button v-if="form.avatar&&form.nickname" class="power_button" open-type="getPhoneNumber"
					@getphonenumber="getPhoneNumber">快捷登录</button>

				<view v-else class="power_button" @click="submit()">手机号快捷登录</view>
			</view>
		</u-popup>

	</view>
</template>

<script>
	import {
		uploadFile
	} from "@/require/api.js";
	export default {
		data() {
			return {
				logs: [],
				show: false,
				type: '',
				uid: '',
				seller_id: '',
				status: '',
				xyList: [],
				check: '',
				img: '',
				form: {
					avatar: '',
					nickname: ''
				},
				logo: '',
				url: ''
			}
		},
		onLoad(e) {
			if (e.type) {
				this.status = e.type;
			}
			if (e.url) {
				this.url = decodeURIComponent(e.url);
			}
			this.seller_id = uni.getStorageSync('seller_id');
			this.getXy();
		},
		methods: {
			setShow() {
				let _ = this;
				uni.login({
					provider: 'weixin',
					success:(res)=>{
					   _.setUser(res.code);
					}
				});

			},

			setUser(code) {

				this.$get(this.$api.islogin, {
					code: code
				}, res => {
					let {
						data,
						code
					} = res.data;
					if (code == 1) {
						
						// if (data) {
						// 	this.toBack(res.data);
						// } else {
							this.show = true;
						//}

					}
				});
			},
			toBack(data) {
				let _ = this;
				uni.setStorageSync('userInfo', data.data);
				uni.setStorageSync('userToken', data.data.token);

				/* if (!data.data.audit_status) {

					uni.reLaunch({
						url: '/pages/new/form'
					}); */

				//} else {
                    

                     console.log(_.url);				

					if (_.status == 1) {
						setTimeout(() => {
							uni.navigateBack({});
						}, 1000)
					} else if (_.status == 2) {
						uni.switchTab({
							url: _.url
						})
					} else if (_.status == 3) {
						uni.redirectTo({
							url: _.url
						})
					} else {
						_.$Toast('登录成功，将跳转到首页');
						setTimeout(() => {
							uni.reLaunch({
								url: '/pages/tabar/index'
							});
						}, 1000)
					}
			//	}
			},
			submit() {
				if (!this.form.avatar) return this.$Toast('请上传头像');

				if (!this.form.nickname) return this.$Toast('请填写昵称');
			},
			async setImg(e) {
				let img = e.detail.avatarUrl;
				console.log(img);
				let data = await uploadFile(img, {});
				data = JSON.parse(data);
				this.img = data.data.fullurl;
				this.form.avatar = data.data.url;
			},
			pageUrl(e) {
				uni.navigateTo({
					url: '/pages/account/treaty?id=' + e.id
				})
			},
			setCheck() {
				this.$Toast('请确认同意协议')
			},
			getXy() {
				let _ = this;
				_.$get(_.$api.NewsXy, {}, (res) => {
					let {
						data
					} = res.data;
					_.xyList = data.list;
					_.logo = data.logo;
				});
			},
			WeChatUserInfo(e) {

				let _ = this;
				uni.getUserProfile({
					desc: '授权确认',
					success(res1) {
						let {
							nickName,
							gender,
							avatarUrl
						} = res1.userInfo;
						uni.login({
							provider: 'weixin',
							success(res) {
								_.$get(_.$api.userWx_login, {
									nickname: nickName,
									headimgurl: avatarUrl,
									code: res.code,
									seller_id: _.seller_id ? _.seller_id : ''
									// seller_id:21
								}, (result) => {

									let {
										data
									} = result;
									if (data.code == 1) {
										getApp().globalData.isReload = true;
										uni.setStorageSync('userInfo', data.data);
										uni.setStorageSync('userToken', data.data.token);

                                           let _=this;
                                        
										if (data.data.mobile) {

											/* if (!data.data.audit_status && data.data.is == 0) {
												uni.reLaunch({
													url: '/pages/new/form'
												});
											} else { */

												if (_.status == 1) {
													setTimeout(() => {
														uni.navigateBack({});
													}, 1000)
												} else if (_.status == 2) {
													uni.switchTab({
														url: _.url
													})
												} else if (_.status == 3) {
													uni.redirectTo({
														url: _.url
													})
												} else {
													_.$Toast('登录成功，将跳转到首页');
													setTimeout(() => {
														uni.reLaunch({
															url: '/pages/tabar/index'
														});
													}, 1000)
												}

											//}

										} else {
											_.show = true;
										}

									} else {
										_.$Toast(data.msg)
									}
								}, true)
							}
						})
					}
				})

			},

			getPhoneNumber(e) {
				let _ = this;
				uni.login({
					provider: 'weixin',
					success(res) {
						let {
							encryptedData,
							iv
						} = e.detail;
						let params = {
							encrytendData: encryptedData,
							iv,
							token: uni.getStorageSync('userToken'),
							code: res.code,
							data: '',
							seller_id: _.seller_id ? _.seller_id : '',
							avatar: _.form.avatar,
							nickname: _.form.nickname
						};
						_.$post(_.$api.userWx_login, params, (result) => {
							let {
								data
							} = result;
							if (data.code == 1) {

								_.toBack(data);

							} else {
								_.$Toast(data.msg)
							}
						})
					}
				})
			}
		}
	}
</script>

<style scoped>
	.box {
		background-color: white;
		min-height: 100vh;
	}

	.logs_img {
		width: 340rpx;
		/* height: 150rpx;
		background: #FFE048; */
		border-radius: 10rpx;
		margin: 0 auto 0;
		padding: 80rpx 0 0 0;
		overflow: hidden;
	}

	.logs_img image {
		width: 100%;
		height: 100%;
		border-radius: 10rpx;
	}

	.logs_name {
		text-align: center;
		font-size: 36rpx;
		color: #333333;
		margin-top: 30rpx;
	}

	.logs_line {
		width: 630rpx;
		height: 2rpx;
		background: #EEEEEE;
		margin: 80rpx auto;
	}

	.logs_text {
		padding: 80rpx 60rpx 30rpx;
		font-size: 30rpx;
		color: #333333;
	}

	.logs_desc {
		padding: 0 60rpx;
		font-size: 30rpx;
		color: #999999;
		display: flex;
		align-items: center;
	}

	.logs_desc text {
		display: inline-block;
		height: 6rpx;
		width: 6rpx;
		border-radius: 50%;
		background: #999;
		display: inline-block;
		margin-right: 10rpx;
	}

	.power_button {
		width: 630rpx !important;
		height: 80rpx;
		text-align: center;
		line-height: 80rpx !important;
		background: #2b72fe;
		border-radius: 45rpx;
		padding: 0;
		font-size: 28rpx;
		color: white;
		margin: 40rpx auto 0 auto;
		font-weight: normal;
	}

	.agree {
		/* position: fixed;
		bottom: 45rpx; */
		width: 100%;
		text-align: center;
		font-size: 28rpx;
		margin-top: 40rpx;
	}

	.ag_box {
		display: flex;
		box-sizing: border-box;
		padding: 0 20rpx;
		position: fixed;
		bottom: 60rpx;
		left: 0;
		justify-content: center;
	}

	.text_box {
		display: flex;
		align-items: center;
		flex-wrap: wrap;
	}

	.icondanxuanfuxuan {
		color: #2b72fe;

	}

	.iconfont {
		margin-right: 10rpx;
	}

	.protocol {
		color: #2b72fe;
	}

	.msgs_box {
		box-sizing: border-box;
		padding: 20rpx 20rpx;
		background-color: white;
		font-size: 35rpx;
	}

	.logo_img {
		width: 80rpx;
		height: 80rpx;
		border-radius: 50%;
		margin-right: 20rpx;
	}

	.msg_head {
		display: flex;
		align-items: center;

		font-weight: bold;
	}

	.ml {
		margin-left: 20rpx;
	}

	.msg_text {
		box-sizing: border-box;
		padding: 45rpx 0 25rpx 0;

	}

	.msg_img {
		width: 80rpx;
		height: 80rpx;
		border-radius: 50%;
		margin-left: 20rpx;
	}

	.tx_box {
		display: flex;
		align-items: center;
		box-sizing: border-box;
		padding: 25rpx 0;
		border-bottom: 1rpx solid #E5E5E5;
	}

	.msg_right {
		margin-left: auto;
	}
</style>